﻿Imports System.Net
Imports TNCBAL
Imports TNCDAL
Imports HTLibrary

Public Class ucContTNC
    Dim db As New TanNamChinhDataContext
    Private m_cont As New MTrucks

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        DataGridViewHelper.DataGridView_InitEvent(dtgCont)
    End Sub

    Private Sub frmDMPhuTung_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        KhoiTao_GiaoDien(True)
        KhoiTaodtgCont()
    End Sub

    Public Sub KhoiTaodtgCont(Optional ByVal NameUpdate As String = "", Optional ByVal Name As String = "")
        Try
            Dim fc = m_cont.GetContListByName(Name)

            Dim source As New BindingSource
            source.DataSource = fc
            BindingNavigator1.BindingSource = source

            dtgCont.DataSource = source
            FormatdtgCont()

            Try
                If NameUpdate = "" Then
                    dtgCont.Rows(0).Selected = True
                    dtgCont.FirstDisplayedScrollingRowIndex = 0
                Else
                    Dim index As Integer = 0
                    For i As Integer = 0 To dtgCont.RowCount - 1
                        If dtgCont.Rows(i).Cells("TruckNumber").Value = NameUpdate Then
                            index = i
                            Exit For
                        End If
                    Next

                    dtgCont.Rows(index).Cells("TruckNumber").Selected = True
                End If
            Catch ex As Exception

            End Try
        Catch ex As Exception

        End Try
    End Sub

    Public Sub FormatdtgCont()
        'dtgCont.RowHeadersWidth = 20
        dtgCont.RowHeadersDefaultCellStyle.ForeColor = Color.Blue

        dtgCont.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill

        dtgCont.MultiSelect = False
        dtgCont.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgCont.ReadOnly = True
        dtgCont.BackgroundColor = Color.White
        dtgCont.AllowUserToAddRows = False
        dtgCont.AllowUserToDeleteRows = False

        dtgCont.Columns("ID_Truck").Visible = False
        dtgCont.Columns("TruckName").Visible = False
        dtgCont.Columns("IsActive").Visible = False
        dtgCont.Columns("Type").Visible = False
        With dtgCont.Columns("TruckNumber")
            .HeaderText = "Cont No"
        End With
    End Sub

#Region "btn Them, Xoa, Sua"
    Dim f_Them As Boolean = False
    Dim f_Sua As Boolean = False

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Clear()
        txtContNo.Focus()
        KhoiTao_GiaoDien(False)
        f_Them = True
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        KhoiTao_GiaoDien(False)
        txtContNo.Focus()
        f_Sua = True
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        Dim ID_Truck As Integer = dtgCont.CurrentRow.Cells("ID_Truck").Value
        'kiem tra truoc khi xoa
        Dim FCName As String = dtgCont.CurrentRow.Cells("TruckNumber").Value
        If (MsgBox("Bạn muốn xóa " + FCName + " ra khỏi danh sách", MsgBoxStyle.YesNo, "Chú ý") = MsgBoxResult.Yes) Then
            Try
                m_cont.UpdateDeletedRecord(ID_Truck)
                KhoiTaodtgCont()
                'write systemlog
                Dim objLogController As New LogController
                objLogController.Insert(enumAction.Delete, String.Format("Cont of TNC No {0}", FCName))
            Catch ex As Exception
                MsgBox("Del - Cont of TNC: " + FCName + " đang được sử dụng. Không xóa được")
            End Try
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim obj_ac As New tblTruck
        obj_ac.TruckNumber = txtContNo.Text
        obj_ac.Type = 2

        'Kiem tra trung
        Dim count = (From p In db.tblTrucks
                     Where p.Type = 2 And p.TruckNumber = txtContNo.Text
                     Select p).Count

        If count > 0 Then
            MsgBox("Đã tồn tại cont Name: " + txtContNo.Text)
            Exit Sub
        End If

        If f_Them Then
            obj_ac.IsActive = True
            m_cont.InsertNewRecord(obj_ac)
            KhoiTaodtgCont(txtContNo.Text, "")
            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Add, String.Format("Cont of TNC No {0}", obj_ac.TruckNumber))
        End If

        If f_Sua Then
            obj_ac.ID_Truck = dtgCont.CurrentRow.Cells("ID_Truck").Value
            Dim index As Integer = dtgCont.CurrentRow.Index
            m_cont.UpdateRecord(obj_ac)
            KhoiTaodtgCont(txtContNo.Text, "")
            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Edit, String.Format("Cont of TNC No {0}", obj_ac.TruckNumber))
        End If

        f_Them = False
        f_Sua = False
        KhoiTao_GiaoDien(True)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        KhoiTao_GiaoDien(True)
        KhoiTaodtgCont(dtgCont.CurrentRow.Cells("TruckNumber").Value, txtNameSearch.Text)
    End Sub

    Public Sub KhoiTao_GiaoDien(ByVal flag As Boolean)
        btnAdd.Enabled = flag
        btnEdit.Enabled = flag
        btnDel.Enabled = flag
        btnUpdate.Enabled = Not flag
        btnCancel.Enabled = Not flag
        txtContNo.ReadOnly = flag

        dtgCont.Enabled = flag

        'If flag Then
        '    txtContNo.BackColor = Color.Azure
        'Else
        '    txtContNo.BackColor = Color.White
        'End If
    End Sub

    Public Sub Clear()
        txtContNo.Text = ""
    End Sub
#End Region

    Private Sub dtgCont_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgCont.CurrentCellChanged
        Try
            txtContNo.Text = dtgCont.CurrentRow.Cells("TruckNumber").Value
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        KhoiTaodtgCont("", txtNameSearch.Text.Trim)
    End Sub

    Private Sub btnCancelSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelSearch.Click
        txtNameSearch.Text = ""
        KhoiTaodtgCont("", txtNameSearch.Text.Trim)
    End Sub

    Private Sub txtNameSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNameSearch.KeyDown
        KhoiTaodtgCont("", txtNameSearch.Text.Trim)
    End Sub

    Private Sub txtContNo_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtContNo.Validating
        If txtContNo.Text.Length <> 11 Then
            MsgBox("Độ dài cont Name phải bằng 11 ký tự")
            e.Cancel = True
        End If
    End Sub
End Class
